package com.fjwt.gz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjwt.gz.db.dto.ScoreRecordDTO;
import com.fjwt.gz.db.entity.ScoreRecord03Entity;
import com.fjwt.gz.db.entity.ScoreRecord03Entity;
import com.fjwt.gz.db.entity.ScoreRecord03Entity;
import com.fjwt.gz.db.entity.ScoreRecord06Entity;
import com.fjwt.gz.interfaces.BaseScoreRecodService;
import com.fjwt.gz.service.mapper.ScoreRecord03Mapper;
import com.fjwt.gz.service.mapper.ScoreRecord03Mapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员积分领取记录明细表（每条记录相当于1积分） 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class ScoreRecord03Service extends ServiceImpl<ScoreRecord03Mapper, ScoreRecord03Entity> implements BaseScoreRecodService {

    @Autowired
    private ScoreRecord03Mapper scoreRecord03Mapper;

    @Override
    public int addScore(List<ScoreRecordDTO> list) {
        List<ScoreRecord03Entity> scoreRecord03Entities = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            ScoreRecord03Entity scoreRecord03Entity = new ScoreRecord03Entity();
            BeanUtils.copyProperties(list.get(i),scoreRecord03Entity);
            scoreRecord03Entities.add(scoreRecord03Entity);
        }
        List<BatchResult> insert = scoreRecord03Mapper.insert(scoreRecord03Entities);
        if (ObjectUtils.isEmpty(insert)) {
            return 0;
        }else {
            return insert.stream().mapToInt(item -> item.getParameterObjects().size()).sum();
        }
    }

    /**
     * 用户主动消耗积分时查询过期积分list
     * @param fansId
     * @param overDate
     * @return
     */
    @Override
    public List<ScoreRecordDTO> selectOffRecordList(Long fansId, String overDate, Integer scoreNeed) {
        return scoreRecord03Mapper.selectOffRecordList(fansId,  overDate, scoreNeed);
    }

    /**
     * 积分明细过期更新
     * @param offScoreRecords
     */
    @Override
    public void updateBatchOffScore(List<ScoreRecordDTO> offScoreRecords, Integer state,Long fansId) {
        scoreRecord03Mapper.updateBatchOffScore(offScoreRecords, state,fansId);
    }

    /**
     * 定时任务过期积分查询
     * @param overDate
     * @param state
     * @return
     */
    @Override
    public IPage<ScoreRecordDTO> selectOffRecordListOfTask(Page page, String overDate, Integer state) {
        return scoreRecord03Mapper.selectOffRecordListOfTask(page, overDate, state);
    }
}
